package org.ethan.pay.controller;

import org.csource.common.MyException;
import org.ethan.basic.exception.BasicException;
import org.ethan.basic.util.AjaxResult;
import org.ethan.pay.dto.AlipayNotifyDto;
import org.ethan.pay.service.IPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 支付业务
 */
@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private IPayService payService;

    // 支付申请
    @PostMapping("/applyPay/{orderNum}")
    public AjaxResult applyPay(@PathVariable("orderNum")String orderNum){
        try {
            // 请求支付申请 , 这个结果是一个HTML代码
            String htmlResult = payService.applyPay(orderNum);
            return AjaxResult.me().setResultObj(htmlResult);
        } catch (BasicException e){
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e){
            return AjaxResult.me().setMessage("系统异常");
        }

    }

    /**
     * 异步通知   http://65cji7.natappfree.cc/pay/notify
     * 谁在调用？支付宝      返回给谁？支付宝?
     * 不用返回json格式
     * @param dto
     * @return
     */
    @RequestMapping("/notify")
    public String alipayNotify(AlipayNotifyDto dto){
        return payService.alipayNotify(dto);
    }


}
